Camera APIs
Camera APIs provide comprehensive functionality for photo capture, permission management, and error handling in universal apps.
Camera APIs
Comprehensive camera functionality for universal apps with photo capture, permission management, and error handling.
useCamera() Hook
Main hook for camera functionality including photo capture, permission handling, and state management.
Property | Type | Description |
---|---|---|
datarequired | Object | null | Captured photo data object containing fileSrc (base64/path), fileName, size, mimeType, transport method, and metadata. |
loadingrequired | boolean | Operation in progress state, true during photo capture, permission requests, or processing. |
error | Object | null | Standardized error object with code, category, message, details, recoverable flag, and suggested action. |
progress | Object | Detailed progress tracking with state, phase, message, percentage, transport method, and byte counts. |
isWeb | boolean | Environment detection flag, true when running in web browser context. |
isNative | boolean | Environment detection flag, true when running in native app (iOS/Android) context. |
executerequired | (operation?: string, options?: Object) => void | Primary function to trigger operations: execute("takePhoto"), execute("requestPermission"), or execute("checkPermission"). |
clear | () => void | Clear data and reset all state (data, error, progress) to initial values. |
clearError | () => void | Clear only the error state while preserving data and other states. |
permission | Object | Camera permission status object with status, canRequest flag, and lastChecked timestamp. |
photo | string | null | Legacy alias for data - Base64 encoded image data (backward compatibility). |
takePhoto | () => void | Legacy alias for execute("takePhoto") - Function to trigger camera capture (backward compatibility). |
clearPhoto | () => void | Legacy alias for clear() - Function to clear photo state (backward compatibility). |
useCameraPermission() Hook
Automatically requests and manages camera permission status with loading states.
Property | Type | Description |
---|---|---|
permissionrequired | string | null | Current camera permission status: GRANTED, DENIED, NOT_DETERMINED, RESTRICTED. Updates automatically when permissions change. |
requestCameraPermission() Function
Promise-based function for explicit camera permission requests with async/await support.
Property | Type | Description |
---|---|---|
Parameters | void | No parameters required. Function automatically requests camera permission from the system. |
Returns | Promise<string> | Promise that resolves with permission status (GRANTED) or rejects with error message if permission denied/failed. |
Platform & Device Behavior
Camera API behavior varies across different platforms and device types. See detailed breakdown below.
Platform | Status | Behavior | Notes |
---|---|---|---|
🤖 Android Emulator | ✅ Supported | Camera works with virtual camera. Photos captured from emulator camera interface. | Uses Android Virtual Device camera simulation |
🤖 Android Physical | ✅ Supported | Full camera functionality with device hardware camera. High-quality photo capture. | Requires camera permission. Works with front/back cameras. |
🍎 iOS Simulator | ✅ Supported | Camera works with simulated camera interface. Stock photos available for testing. | Uses iOS Simulator camera simulation with sample images |
🍎 iOS Physical | ⏳ Coming Soon | Physical device camera support currently in development. | Will support device hardware camera when implemented |
🌐 Web Browser | 🔄 Fallback | Returns safe defaults. Camera functions available but no actual camera access. | Graceful degradation for web platform compatibility |
📸 Avatar Photo Capture Example
Capture and display user avatars with real-time camera access and state management
🎛️ Customize Your Example
Select which hooks and properties to include in your example. All hooks follow a common interface with standardized properties and operations.
1import React from 'react';2import { useCamera } from "catalyst-core/hooks";34function CameraApp() {5 const {6 // New standardized interface7 data: photoData,8 execute: executeCamera9 } = useCamera();1011 const formatFileSize = (bytes) => {12 if (bytes === 0) return '0 Bytes'13 const k = 102414 const sizes = ['Bytes', 'KB', 'MB', 'GB']15 const i = Math.floor(Math.log(bytes) / Math.log(k))16 return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]17 }1819 return (20 <div style={{ padding: '20px' }}>21 <h2>📷 Camera Demo</h2>2223 {/* Camera Controls */}2425 <button onClick={executeCamera} >26 "Take Photo"27 </button>28 </div>2930 {photoData && (31 <div>32 <h3>Photo Captured!</h3>33 <p>Name: {photoData.fileName}</p>34 <p>Size: {formatFileSize(photoData.size)}</p>35 <img src={photoData.fileSrc} alt="Captured" style={{ maxWidth: '300px' }} />3637 </div>38 )}39 </div>40 );41}4243export default CameraApp;
Important Notes
- Platform Support: Android (emulator + physical) and iOS (simulator) supported. iOS physical device coming soon
- Parameters: Camera functions require no parameters - all configuration is handled automatically
- Emulator/Simulator: Camera works in Android emulator and iOS simulator with virtual/sample images
- Permissions: Always check permission status before capturing photos - required on physical devices
- Data Format: Photos returned as base64 JPEG strings ready for immediate display or upload
- Error Handling: Built-in error handling for permission denied, camera unavailable, and capture failures
- Web Fallback: Returns safe defaults when running in web mode for graceful degradation